$(function () {
    var infomation_data = []; //所有数据
    var search_info_data = [];//查询按钮查出的数据
    var checkPopLayer = null;//弹窗对象
    var tableObject = null;//表格对象
    var title = "单位综合信息"; //excel的标题
    var isSearchAll = true;
    var mainId = "";

    //弹窗内文本对象
    var layer_computer_number = $("#layer-computer-number");
    var layer_unit_number = $("#layer-unit-number");
    var layer_unit_name = $("#layer-unit-name");
    var layer_zhongxin = $("#layer-zhongxin");
    var layer_member = $("#layer-member");
    var layer_unit_type = $("#layer-unit-type");
    var layer_hangye = $("#layer-hangye");
    var layer_tell = $("#layer-tell");
    var layer_post_number = $("#layer-post-number");
    var layer_tongxun = $("#layer-tongxun");
    var layer_zhanghu = $("#layer-zhanghu");
    var layer_yinhang = $("#layer-yinhang");
    var layer_leixing = $("#layer-leixing");
    var layer_post_haoma = $("#layer-post-haoma");
    var layer_fafang = $("#layer-fafang");
    var layer_qixian = $("#layer-qixian");
    var layer_pzdanwei = $("#layer-pzdanwei");
    var layer_pzri = $("#layer-pzriqi");
    var layer_pzwenhao = $("#layer-pzwenhao");
    //测试数据
    infomation_data = [
        {
            id: 203,
            unitNumber: '104008',
            unitName: '广州市美的集团有限公司',
            computerNumber: '3504250196548',
            unitType: '城镇国有企业',
            membership: '县（区）',
            insurancekind: '医疗保险',
            hangye:'销售服务',
            zhongxin: '广州市天河区',
            tell: '18900242680',
            postNumber: 350000,
            tongxun: '天河区车陂街中山大道西55号华师大南区12栋',
            zhanghu: '',
            yinhang: '',
            zhonglei: '',
            haoma: '',
            fafang: '',
            qixian: '',
            pzdanwei: '',
            pzriqi: '',
            pzwenhao: ''
        }, {
            id: 204,
            unitNumber: '2382511',
            unitName: 'TCL集团股份有限公司',
            computerNumber: '3504255243126',
            unitType: '上市企业',
            membership: '县（区）',
            insurancekind: '生育保险',
            hangye:'销售服务',
            zhongxin: '广东省惠州市',
            tell: '2288389',
            postNumber: 516001,
            tongxun: '广东省惠州市仲恺高新技术产业开发区十九号小区',
            zhanghu: '',
            yinhang: '',
            zhonglei: '企业营业执照',
            haoma: '',
            fafang: '2001-03-09',
            qixian: '20年',
            pzdanwei: '',
            pzriqi: '',
            pzwenhao: ''
        }, {
            id: 205,
            unitNumber: '2261868',
            unitName: 'TCL通讯设备股份有限公司',
            computerNumber: '3504250196548',
            hangye:'销售服务',
            unitType: '上市企业',
            membership: '县（区）',
            insurancekind: '工伤保险',
            zhongxin: '广东省惠州市',
            tell: '2288863',
            postNumber: 516001,
            tongxun: '广东省惠州市上排大岭路10号TCL大厦',
            zhanghu: '',
            yinhang: '',
            zhonglei: '企业营业执照',
            haoma: '',
            fafang: '',
            qixian: '',
            pzdanwei: '',
            pzriqi: '',
            pzwenhao: ''
        }, {
            id: 206,
            unitNumber: '83433331',
            unitName: '北方国际合作股份有限公司',
            computerNumber: '3504250196548',
            hangye:'外贸',
            unitType: '上市企业',
            membership: '县（区）',
            insurancekind: '医疗保险',
            zhongxin: '广东省深圳市福田区',
            tell: '83434489*6611',
            postNumber: 350000,
            tongxun: '广东省深圳市福田区天安数码城B座910-912室',
            zhanghu: '',
            yinhang: '',
            zhonglei: '企业营业执照',
            haoma: '',
            fafang: '2009-04-12',
            qixian: '5年',
            pzdanwei: '',
            pzriqi: '',
            pzwenhao: ''
        }, {
            id: 207,
            unitNumber: '8909353',
            unitName: '东莞发展控股股份有限公司',
            computerNumber: '3504250196548',
            unitType: '上市企业',
            hangye:'金融',
            membership: '县（区）',
            insurancekind: '养老保险',
            zhongxin: '广东省东莞市东城区莞樟',
            tell: '2083320',
            postNumber: 523110,
            tongxun: '广东省东莞市东城区莞樟大道55高',
            zhanghu: '',
            yinhang: '',
            zhonglei: '企业营业执照',
            haoma: '',
            fafang: '',
            qixian: '',
            pzdanwei: '',
            pzriqi: '',
            pzwenhao: ''
        }
    ];

    //layui的使用  可参考官方api
    layui.use(['form', 'layer','element'], function () {
        var form = layui.form,
            layer = layui.layer,
            element = layui.element;

        //监听提交
        form.on("submit(demo1)", function (data) {
            search_info_data = [];
            var searchLoad = layer.load();
            var submitData = data.field;
            if(submitData.unitNumber || submitData.unitName || submitData.computerNumber){
                isSearchAll = false;
            }else{
                isSearchAll = true;
            }
            //遍历查询数据
            $.each(infomation_data,function(index,item){
                if(submitData.unitNumber && submitData.unitNumber == item.unitNumber){
                    search_info_data.push(item);
                }
            });

            //销毁表格对象
            if(tableObject != null){
                tableObject.clear();
                tableObject.destroy();
            }
            initTable();
            setTimeout(function(){
                layer.close(searchLoad);
            },500);
            return false;
        });

        initTable();

    });
    //初始化表格
    function initTable() {
        tableObject = $("#info_table").DataTable({
            searching: false,
            ordering: false,
            lengthChange: false,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [
                {data: 'unitNumber'},
                {data: 'unitName'},
                {data: 'computerNumber'},
                {data: 'unitType'},
                {data: 'membership'},
                {data: 'insurancekind'},
                {
                    data: 'id', render: function (data, type, row, meta) {
                    return "<button class='layui-btn layui-btn-normal disaccord' value='" + data + "'>查看详情</button>";
                }
                }
            ],
            data: isSearchAll?infomation_data:search_info_data,
            //导出
            dom: 'Bfrtip',
            buttons: [
                {extend: 'excel', text: '导出为EXCEL', title: title},
                {extend: 'csv', text: '导出为csv', title: title},
                {extend: 'print', text: '打印', title: title}
            ]
        });

        $(".disaccord").on("click",function(){
            //清空文本内容
            layer_computer_number.html('');
            layer_unit_number.html('');
            layer_unit_name.html('');
            layer_zhongxin.html('');
            layer_member.html('');
            layer_unit_type.html('');
            layer_hangye.html('');
            layer_tell.html('');
            layer_post_number.html('');
            layer_tongxun.html('');
            layer_zhanghu.html('');
            layer_yinhang.html('');
            layer_leixing.html('');
            layer_post_haoma.html('');
            layer_fafang.html('');
            layer_qixian.html('');
            layer_pzdanwei.html('');
            layer_pzri.html('');
            layer_pzwenhao.html('');
            //匹配点击的item
            var id= $(this).attr("value");
            $.each(infomation_data,function(index,data){
                if(id == data.id){
                    layer_computer_number.html(data.computerNumber);
                    layer_unit_number.html(data.unitNumber);
                    layer_unit_name.html(data.unitName);
                    layer_zhongxin.html(data.zhongxin);
                    layer_member.html(data.membership);
                    layer_unit_type.html(data.unitType);
                    layer_hangye.html(data.hangye);
                    layer_tell.html(data.tell);
                    layer_post_number.html(data.postNumber);
                    layer_tongxun.html(data.tongxun);
                    layer_zhanghu.html(data.zhanghu);
                    layer_yinhang.html(data.yinhang);
                    layer_leixing.html(data.zhonglei);
                    layer_post_haoma.html(data.haoma);
                    layer_fafang.html(data.fafang);
                    layer_qixian.html(data.qixian);
                    layer_pzdanwei.html(data.pzdanwei);
                    layer_pzri.html(data.pzriqi);
                    layer_pzwenhao.html(data.pzwenhao);
                    return;
                }
            });
            checkPopLayer = layer.open({
                type: 1,
                content: $("#layer-check-pop"),
                area: ['1000px', '480px'],
                title: '查看',
                maxmin:true,
                cancel: function () {
                }
            });
        });
    };
    //初始化页面
    /*function init() {
        initTable();
    };
    init();*/
});